/**
 * 
 */
package misc;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;

/**
 * @author haiyuzha
 * 
 */
public class PopupTriggerListener extends MouseAdapter {
	private JPopupMenu menu = null;

	public PopupTriggerListener() {
		menu = new JPopupMenu("Popup");
		JMenuItem item = new JMenuItem("New");
		item.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.out.println("Menu item New");
			}
		});
		menu.add(item);

		item = new JMenuItem("Delete");
		item.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.out.println("Menu item Delete");
			}
		});
		menu.add(item);
	}

	public PopupTriggerListener(JPopupMenu m) {
		menu = m;
	}

	public void mousePressed(MouseEvent ev) {
		if (ev.isPopupTrigger()) {
			menu.show(ev.getComponent(), ev.getX(), ev.getY());
		}
	}

	public void mouseReleased(MouseEvent ev) {
		if (ev.isPopupTrigger()) {
			menu.show(ev.getComponent(), ev.getX(), ev.getY());
		}
	}
}
